﻿using Google.ProtocolBuffers;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace LightingServer
{
    public class LightingCreator
    {
        public void Create(DbConnection conn, Lighting lighting)
        {
            CreatePysical(conn.CreateCommand(), lighting.Physical);
            CreateOptics(conn.CreateCommand(), lighting.Optics);
            CreateElectric(conn.CreateCommand(), lighting.Electric);
            CreateAdapterLighting(conn.CreateCommand(), lighting.AdapterLighting);
            CreateProject(conn.CreateCommand(), lighting.Project);
            CreateBusiness(conn.CreateCommand(), lighting.Business);
            CreatePicture(conn.CreateCommand(), lighting.Picture);

            StringBuilder prefixBuilder = new StringBuilder();
            prefixBuilder.Append("INSERT INTO lighting(id");

            List<DbParameter> parameters = new List<DbParameter>();
            StringBuilder suffixBuilder = new StringBuilder();
            suffixBuilder.Append("(@id");
            DbCommand command = conn.CreateCommand();
            command.Parameters.Add(new  MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = lighting.Id
            });

            if (lighting.HasSeries)
            {
                prefixBuilder.Append(",series");
                suffixBuilder.Append(",@series");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@series",
                    DbType = DbType.String,
                    Value = lighting.Series
                });
            }

            if (lighting.HasClass)
            {
                prefixBuilder.Append(",class");
                suffixBuilder.Append(",@class");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@class",
                    DbType = DbType.UInt32,
                    Value = lighting.Class
                });
            }

            if (lighting.HasPlace)
            {
                prefixBuilder.Append(",place");
                suffixBuilder.Append(",@place");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@place",
                    DbType = DbType.UInt32,
                    Value = lighting.Place
                });
            }

            if (lighting.HasVersion)
            {
                prefixBuilder.Append(",version");
                suffixBuilder.Append(",@version");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@version",
                    DbType = DbType.String,
                    Value = lighting.Version
                });
            }

            if (lighting.HasLable)
            {
                prefixBuilder.Append(",lable");
                suffixBuilder.Append(",@lable");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@lable",
                    DbType = DbType.String,
                    Value = lighting.Lable
                });
            }

            if (lighting.HasOrgin)
            {
                prefixBuilder.Append(",orgin");
                suffixBuilder.Append(",@orgin");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@orgin",
                    DbType = DbType.UInt32,
                    Value = lighting.Orgin
                });
            }

            prefixBuilder.Append(",physicalID");
            suffixBuilder.Append(",@physicalID");

            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@physicalID",
                DbType = DbType.String,
                Value = lighting.Physical.Id
            });

            prefixBuilder.Append(",opticsID");
            suffixBuilder.Append(",@opticsID");

            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@opticsID",
                DbType = DbType.String,
                Value = lighting.Optics.Id
            });

            prefixBuilder.Append(",electricID");
            suffixBuilder.Append(",@electricID");

            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@electricID",
                DbType = DbType.String,
                Value = lighting.Electric.Id
            });

            prefixBuilder.Append(",adapterID");
            suffixBuilder.Append(",@adapterID");

            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@adapterID",
                DbType = DbType.String,
                Value = lighting.AdapterLighting.Id
            });

            prefixBuilder.Append(",projectID");
            suffixBuilder.Append(",@projectID");

            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@projectID",
                DbType = DbType.String,
                Value = lighting.Project.Id
           });

            prefixBuilder.Append(",businessID");
            suffixBuilder.Append(",@businessID");

            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@businessID",
                DbType = DbType.String,
                Value = lighting.Business.Id
            });

            prefixBuilder.Append(",pictureID");
            suffixBuilder.Append(",@pictureID");

            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@pictureID",
                DbType = DbType.String,
                Value = lighting.Picture.Id
            });

            prefixBuilder.Append(") VALUES ");
            suffixBuilder.Append(");");

            command.CommandText = prefixBuilder.ToString() + suffixBuilder.ToString();
            command.ExecuteNonQuery();
        }

        private void CreatePysical(DbCommand command,Physical physical)
        {
            StringBuilder prefixBuilder = new StringBuilder();
            prefixBuilder.Append("INSERT INTO physical(id");

            List<DbParameter> parameters = new List<DbParameter>();
            StringBuilder suffixBuilder = new StringBuilder();
            suffixBuilder.Append("(@id");
            command.Parameters.Add(new MySqlParameter() 
            { 
                ParameterName = "@id", 
                DbType = DbType.String,
                Value = physical.Id
            });

            if(physical.HasMaterial)
            {
                prefixBuilder.Append(",material");
                suffixBuilder.Append(",@material");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName =  "@material",
                    DbType = DbType.String,
                    Value = physical.Material
                });
            }

            if(physical.HasColor)
            {
                prefixBuilder.Append(",color");
                suffixBuilder.Append(",@color");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName =  "@color",
                    DbType = DbType.String,
                    Value = physical.Color
                });
            }

            if(physical.HasSize)
            {
                prefixBuilder.Append(",size");
                suffixBuilder.Append(",@size");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName =  "@size",
                    DbType = DbType.String,
                    Value = physical.Size
                });
            }

            if(physical.HasDefend)
            {
                prefixBuilder.Append(",defend");
                suffixBuilder.Append(",@defend");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName =  "@defend",
                    DbType = DbType.String,
                    Value = physical.Defend
                });
            }

            if(physical.HasImpact)
            {
                prefixBuilder.Append(",impact");
                suffixBuilder.Append(",@impact");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName =  "@impact",
                    DbType = DbType.UInt32,
                    Value = physical.Impact
                });
            }

            if(physical.HasWeight)
            {
                prefixBuilder.Append(",weight");
                suffixBuilder.Append(",@weight");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName =  "@weight",
                    DbType = DbType.String,
                    Value = physical.Weight
                });
            }

            prefixBuilder.Append(") VALUES ");
            suffixBuilder.Append(");");

            command.CommandText = prefixBuilder.ToString() + suffixBuilder.ToString();
            command.ExecuteNonQuery();
        }

        private void CreateOptics(DbCommand command, Optics optics)
        {
            StringBuilder prefixBuilder = new StringBuilder();
            prefixBuilder.Append("INSERT INTO optics(id");

            List<DbParameter> parameters = new List<DbParameter>();
            StringBuilder suffixBuilder = new StringBuilder();
            suffixBuilder.Append("(@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = optics.Id
            });

            if (optics.HasExplain)
            {
                prefixBuilder.Append(",explain1");
                suffixBuilder.Append(",@explain1");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@explain1",
                    DbType = DbType.UInt32,
                    Value = optics.Explain
                });
            }

            if (optics.HasAngle)
            {
                prefixBuilder.Append(",angle");
                suffixBuilder.Append(",@angle");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@angle",
                    DbType = DbType.Double,
                    Value = optics.Angle
                });
            }


            if (optics.HasEfficiency)
            {
                prefixBuilder.Append(",efficiency");
                suffixBuilder.Append(",@efficiency");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@efficiency",
                    DbType = DbType.Double,
                    Value = optics.Efficiency
                });
            }

            prefixBuilder.Append(") VALUES ");
            suffixBuilder.Append(");");

            command.CommandText = prefixBuilder.ToString() + suffixBuilder.ToString();
            command.ExecuteNonQuery();
        }

        private void CreateElectric(DbCommand command, Electric electric)
        {
            StringBuilder prefixBuilder = new StringBuilder();
            prefixBuilder.Append("INSERT INTO electric(id");

            List<DbParameter> parameters = new List<DbParameter>();
            StringBuilder suffixBuilder = new StringBuilder();
            suffixBuilder.Append("(@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = electric.Id
            });

            if (electric.HasShock)
            {
                prefixBuilder.Append(",shock");
                suffixBuilder.Append(",@shock");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@shock",
                    DbType = DbType.String,
                    Value = electric.Shock
                });
            }

            if (electric.HasVoltage)
            {
                prefixBuilder.Append(",voltage");
                suffixBuilder.Append(",@voltage");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@voltage",
                    DbType = DbType.UInt32,
                    Value = electric.Voltage
                });
            }

            if (electric.HasType)
            {
                prefixBuilder.Append(",type");
                suffixBuilder.Append(",@type");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@type",
                    DbType = DbType.String,
                    Value = electric.Type
                });
            }

            if (electric.HasElectricPower)
            {
                prefixBuilder.Append(",electricPower");
                suffixBuilder.Append(",@electricPower");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@electricPower",
                    DbType = DbType.UInt32,
                    Value = electric.ElectricPower
                });
            }

            if (electric.HasLightingPower)
            {
                prefixBuilder.Append(",lightingPower");
                suffixBuilder.Append(",@lightingPower");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@lightingPower",
                    DbType = DbType.UInt32,
                    Value = electric.LightingPower
                });
            }

            if (electric.HasAllPower)
            {
                prefixBuilder.Append(",allPower");
                suffixBuilder.Append(",@allPower");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@allPower",
                    DbType = DbType.UInt32,
                    Value = electric.AllPower
                });
            }

            if (electric.HasControlType)
            {
                prefixBuilder.Append(",controlType");
                suffixBuilder.Append(",@controlType");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@controlType",
                    DbType = DbType.UInt32,
                    Value = electric.ControlType
                });
            }

            prefixBuilder.Append(") VALUES ");
            suffixBuilder.Append(");");

            command.CommandText = prefixBuilder.ToString() + suffixBuilder.ToString();
            command.ExecuteNonQuery();
        }

        private void CreateAdapterLighting(DbCommand command, AdapterLighting adapterLighting)
        {
            StringBuilder prefixBuilder = new StringBuilder();
            prefixBuilder.Append("INSERT INTO adapterLighting(id");

            List<DbParameter> parameters = new List<DbParameter>();
            StringBuilder suffixBuilder = new StringBuilder();
            suffixBuilder.Append("(@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = adapterLighting.Id
            });

            if (adapterLighting.HasClass)
            {
                prefixBuilder.Append(",class");
                suffixBuilder.Append(",@class");

                command.Parameters.Add(new MySqlParameter()
                {
                     ParameterName = "@class",
                     DbType = DbType.UInt32,
                     Value = adapterLighting.Class
                });
            }

            if (adapterLighting.HasVersion)
            {
                prefixBuilder.Append(",version");
                suffixBuilder.Append(",@version");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@version",
                    DbType = DbType.String,
                    Value = adapterLighting.Version
                });
            }

            if (adapterLighting.HasColor)
            {
                prefixBuilder.Append(",color");
                suffixBuilder.Append(",@color");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@color",
                    DbType = DbType.String,
                    Value = adapterLighting.Color
                });
            }

            if (adapterLighting.HasCri)
            {
                prefixBuilder.Append(",cri");
                suffixBuilder.Append(",@cri");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@cri",
                    DbType = DbType.UInt32,
                    Value = adapterLighting.Cri
                });
            }

            if (adapterLighting.HasOutput)
            {
                prefixBuilder.Append(",output");
                suffixBuilder.Append(",@output");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@output",
                    DbType = DbType.UInt32,
                    Value = adapterLighting.Output
                });
            }

            if (adapterLighting.HasPins)
            {
                prefixBuilder.Append(",pins");
                suffixBuilder.Append(",@pins");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@pins",
                    DbType = DbType.String,
                    Value = adapterLighting.Pins
                });
            }

            prefixBuilder.Append(") VALUES ");
            suffixBuilder.Append(");");

            command.CommandText = prefixBuilder.ToString() + suffixBuilder.ToString();
            command.ExecuteNonQuery();
        }

        private void CreateProject(DbCommand command, Project project)
        {
            StringBuilder prefixBuilder = new StringBuilder();
            prefixBuilder.Append("INSERT INTO project(id");

            List<DbParameter> parameters = new List<DbParameter>();
            StringBuilder suffixBuilder = new StringBuilder();
            suffixBuilder.Append("(@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = project.Id
            });

            if (project.HasParts)
            {
                prefixBuilder.Append(",parts");
                suffixBuilder.Append(",@parts");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@parts",
                    DbType = DbType.String,
                    Value = project.Parts
                });
            }

            if (project.HasSetup)
            {
                prefixBuilder.Append(",setup");
                suffixBuilder.Append(",@setup");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@setup",
                    DbType = DbType.UInt32,
                    Value = project.Setup
                });
            }

            if (project.HasSize)
            {
                prefixBuilder.Append(",size");
                suffixBuilder.Append(",@size");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@size",
                    DbType = DbType.String,
                    Value = project.Size
                });
            }

            if (project.HasReport)
            {
                prefixBuilder.Append(",report");
                suffixBuilder.Append(",@report");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@report",
                    DbType = DbType.String,
                    Value = project.Report
                });
            }

            if (project.HasBaikup)
            {
                prefixBuilder.Append(",baikup");
                suffixBuilder.Append(",@baikup");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@baikup",
                    DbType = DbType.String,
                    Value = project.Baikup
                });
            }

            prefixBuilder.Append(") VALUES ");
            suffixBuilder.Append(");");

            command.CommandText = prefixBuilder.ToString() + suffixBuilder.ToString();
            command.ExecuteNonQuery();
        }

        private void CreateBusiness(DbCommand command, Business business)
        {
            StringBuilder prefixBuilder = new StringBuilder();
            prefixBuilder.Append("INSERT INTO business(id");

            List<DbParameter> parameters = new List<DbParameter>();
            StringBuilder suffixBuilder = new StringBuilder();
            suffixBuilder.Append("(@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = business.Id
            });

            if (business.HasIn)
            {
                prefixBuilder.Append(",in");
                suffixBuilder.Append(",@in");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@in",
                    DbType = DbType.Double,
                    Value = business.In
                });
            }

            if (business.HasMarket)
            {
                prefixBuilder.Append(",market");
                suffixBuilder.Append(",@market");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@market",
                    DbType = DbType.Double,
                    Value = business.Market
                });
            }

            if (business.HasOut)
            {
                prefixBuilder.Append(",out");
                suffixBuilder.Append(",@out");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@out",
                    DbType = DbType.Double,
                    Value = business.Out
                });
            }

            if (business.HasDelievery && !string.IsNullOrEmpty(business.Delievery))
            {
                prefixBuilder.Append(",delievery");
                suffixBuilder.Append(",@delievery");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@delievery",
                    DbType = DbType.String,
                    Value = business.Delievery
                });
            }

            prefixBuilder.Append(") VALUES ");
            suffixBuilder.Append(");");

            command.CommandText = prefixBuilder.ToString() + suffixBuilder.ToString();
            command.ExecuteNonQuery();
        }

        private void CreatePicture(DbCommand command, Picture picture)
        {
            StringBuilder prefixBuilder = new StringBuilder();
            prefixBuilder.Append("INSERT INTO picture(id");

            List<DbParameter> parameters = new List<DbParameter>();
            StringBuilder suffixBuilder = new StringBuilder();
            suffixBuilder.Append("(@id");
            command.Parameters.Add(new MySqlParameter()
            {
                ParameterName = "@id",
                DbType = DbType.String,
                Value = picture.Id
            });

            if (picture.HasFacade)
            {
                prefixBuilder.Append(",facade");
                suffixBuilder.Append(",@facade");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@facade",
                    DbType = DbType.String,
                    Value = picture.Facade
                });
            }

            if (picture.HasSize)
            {
                prefixBuilder.Append(",size");
                suffixBuilder.Append(",@size");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@size",
                    DbType = DbType.String,
                    Value = picture.Size
                });
            }

            if (picture.HasGrading1)
            {
                prefixBuilder.Append(",grading1");
                suffixBuilder.Append(",@grading1");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@grading1",
                    DbType = DbType.String,
                    Value = picture.Grading1
                });
            }

            if (picture.HasLive)
            {
                prefixBuilder.Append(",live");
                suffixBuilder.Append(",@live");

                command.Parameters.Add(new MySqlParameter()
                {
                    ParameterName = "@live",
                    DbType = DbType.String,
                    Value = picture.Live
                });
            }

            prefixBuilder.Append(") VALUES ");
            suffixBuilder.Append(");");

            command.CommandText = prefixBuilder.ToString() + suffixBuilder.ToString();
            command.ExecuteNonQuery();
        }
    }
}
